#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tabwin1.h"
#include "ui_tabwin1.h"
#include <QTabBar>
#include <QToolBar>

using namespace cv;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tabWin1 = new TabWin1(ui->tabWidget_2);
    tabWin1->show();
#ifndef WIN32
    QToolBar *toolBar=new QToolBar;
    toolBar->addActions(ui->menubar->actions());
    this->addToolBar(toolBar);
    ui->menubar->clear();
    // // // //刷新界面
    QTimer *timer = new QTimer(this);
    timer->setInterval(100);
    connect(timer, &QTimer::timeout, [=]{
        this->update();
    });
    timer->start();
#else
    tabWin1->getUi()->pushButton_4->hide();
    tabWin1->getUi()->pushButton_5->hide();
    tabWin1->getUi()->horizontalSpacer->changeSize(0,0);
#endif
    Permission();
    connect(ui->tabWidget_2->tabBar(), &QTabBar::tabCloseRequested, this, &MainWindow::on_tabCloseRequested);
    //// 获取OpenCV的构建信息
    // std::string buildInformation = cv::getBuildInformation();
    // std::cout << "OpenCV build info:\n" << buildInformation << std::endl;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_tabCloseRequested(int index)
{
    if(ui->tabWidget_2->widget(index)!=tabWin1)
        ui->tabWidget_2->widget(index)->deleteLater();
}

void MainWindow::setEdgeMethod(int methodTmp){
    ui->actionnull->setChecked((methodTmp==0)?1:0);
    ui->actionPrewitt->setChecked((methodTmp==1)?1:0);
    ui->actionSobel->setChecked((methodTmp==2)?1:0);
    ui->actionCanny->setChecked((methodTmp==3)?1:0);
    ui->actionLaplacian->setChecked((methodTmp==4)?1:0);
    tabWin1->setEdgeMethod(methodTmp);
}

void MainWindow::on_actionnull_triggered()
{
    setEdgeMethod(0);
}

void MainWindow::on_actionPrewitt_triggered()
{
    setEdgeMethod(1);
}


void MainWindow::on_actionSobel_triggered()
{
    setEdgeMethod(2);
}


void MainWindow::on_actionCanny_triggered()
{
    setEdgeMethod(3);
}


void MainWindow::on_actionLaplacian_triggered()
{
    setEdgeMethod(4);
}

// 顶置窗口
void MainWindow::on_menu1_action1_triggered(bool checked)
{
    if(checked)
        this->setWindowFlags(this->windowFlags()|Qt::WindowStaysOnTopHint);
    else
        this->setWindowFlags(this->windowFlags()&~Qt::WindowStaysOnTopHint);
    this->show();
}

// 销毁所有cv窗口
void MainWindow::on_actionclose_triggered()
{
    for (int i = 0; i < ui->tabWidget_2->count(); ++i) {
        on_tabCloseRequested(i);
    }
    try {
        cv::destroyAllWindows();
    }catch (...) {
    }
}
// 重置所有cv窗口
void MainWindow::on_actionchognzhi_triggered()
{
    // foreach (QString var, open_cv_windows) {
    //     try {
    //         cv::moveWindow(var.toStdString(), 0, 0);
    //     } catch (...) {
    //         open_cv_windows.remove(var);
    //     }
    // }
}

void MainWindow::on_actionqingkong_triggered()
{
    switch (tabWin1->getUi()->tabWidget->currentIndex()) {
    case 0:
        tabWin1->standItemModel->clear();
        break;
    case 1:
        tabWin1->standItemModel2->clear();
        break;
    default:
        break;
    }
}


void MainWindow::on_actionaddRow_triggered()
{
    switch (tabWin1->getUi()->tabWidget->currentIndex()) {
    case 0:
        tabWin1->standItemModel->setItem(tabWin1->standItemModel->rowCount(), new QStandardItem(""));
        break;
    case 1:
        tabWin1->standItemModel2->setItem(tabWin1->standItemModel2->rowCount(), new QStandardItem(""));
        break;
    default:
        break;
    }
}
